package Images;

import org.eclipse.swt.graphics.*;

import Resizers.*;
import Drawers.*;
import Interfaces.*;
import Utils.*;

public class MemoryImage implements Resizable
{
	private ImageData _original;
	private Resizer _resizer;
	private Drawer _drawer;
	
	public void Open(String fileName)
	{
		ImageData temp = new ImageData(fileName);
		_original = new ImageData(temp.width, temp.height, 24, Functions.ClearPalette());
		
		for (int x = 0; x < temp.width; x++)
			for (int y = 0; y < temp.height; y++)
				_original.setPixel(x, y, _original.palette.getPixel(temp.palette.getRGB(temp.getPixel(x, y))));
	}
	
	public void Save(String fileName, int type, Point size)
	{
		_resizer.Resize(_original, new FileDrawer(fileName, type), size);
	}
	
	public void ResizeOriginal(Point size)
	{
		MemoryDrawer tempDrawer = new MemoryDrawer();
		Resize(size, tempDrawer);
		_original = (ImageData)tempDrawer.GetMemory().clone();
	}
	
	public void Resize(Point size, Drawer drawer)
	{
		if (_original != null)
			_resizer.Resize(_original, drawer, size);
	}
	
	public Point OriginalSize()
	{
		return new Point(_original.width, _original.height);
	}
	
	public void Resize(Point size)
	{
		Resize(size, _drawer);
	}
	
	public void setDrawer(Drawer drawer)
	{
		_drawer = drawer;
	}
	
	public void setResizer(Resizer newResizer)
	{
		_resizer = newResizer;
	}
	
	public ImageData GetOriginalImage()
	{
		return _original;
	}
	
	public Point CanResize(Point requestedSize)
	{
		if (_resizer == null)
			return new Point(0,0);
		
		return _resizer.CanResize(requestedSize);
	}
}
